import Vue from 'vue'
import Vuex from 'vuex'
import storage from 'store'
import {getInfo, login, getAnnualReport} from "@/api/login";

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        token: '',
        info: {},
        hasGetInfo: false,
        annualData: {}
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_INFO: (state, info) => {
            state.info = info
        },
        SET_ANNUAL: (state, data) => {
            state.annualData = data
        },
        RESET_INFO: (state) => {
            state.token = ''
            state.info = {}
            state.annualData = {}
        },
        SET_HAS_GET_INFO: (state, hasGetInfo) => {
            state.hasGetInfo = hasGetInfo
        }
    },
    getters: {
        name: state => state.info.name,
        hasGetInfo: state => state.hasGetInfo
    },
    actions: {
        login ({commit}, params) {
            return new Promise((resolve, reject) => {
                login(params).then(res => {
                    const data = res.data
                    storage.set('token_2025', data['accessToken'])
                    commit('SET_TOKEN', data['accessToken'])
                    resolve()
                }).catch(err => {
                    reject(err)
                })
            })
        },
        LoginByToken ({ commit }, token) {
            return new Promise((resolve) => {
                storage.set('token_2025', token)
                commit('SET_TOKEN', token)
                resolve()
            })
        },
        // 模拟数据，实际不能用，男1女2，老师1学生2
        setInfo({ commit }) {
            const info = {
                type: 2,
                gender: 1,
                personType: 7
            }
            const annualData = {
                    data1: 'xxx',
                    data2: 'xxx',
                    data3: 'xxx',
                    data4: 'xxx',
                    data5: 'xxx',
                    data6: 'xxx',
                    data7: 'xxx',
                    data8: 'xxx',
                    data9: 'xxx',
                    data10: 'xxx',
                    data11: 'xxx',
                    data12: 'xxx',
                    data13: 'xxx',
                    data14: 'xxx',
                    data15: 'xxx',
                    data16: 'xxx',
                    data17: 'xxx',
                    data18: 'xxx',
                    data19: 'xxx',
                    data20: 'xxx',
                    data21: 'xxx',
                    data22: 'xxx',
                    data23: 'xxx',
                    data24: 'xxx',
                    data25: 'xxx',
                    data26: 'xxx',
                    data27: 'xxx'
            }
            commit('SET_HAS_GET_INFO', true)
            commit('SET_INFO', info)
            commit('SET_ANNUAL', annualData)
        },
        getInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    console.log('info', res)
                    commit('SET_HAS_GET_INFO', true)
                    commit('SET_INFO', res.data)
                    console.log('login', this.state.hasGetInfo)
                    resolve()
                }).catch(err => {
                    reject(err)
                })
            })
        },
        // 获取年度账单数据
        getAnnualData({ commit }, account) {
            return new Promise((resolve, reject) => {
                getAnnualReport(account).then(res => {
                    console.log('annual', res)
                    commit('SET_ANNUAL', res.data)
                    resolve()
                }).catch(err => {
                    reject(err)
                })
            })
        },
        // 登出
        Logout ({ commit }) {
            commit('RESET_INFO')
            storage.set('token_2025', '')
            /* return new Promise((resolve) => {
                logout().then(() => {
                    commit('RESET_INFO')
                    resolve()
                }).catch(() => {
                    commit('RESET_INFO')
                    resolve()
                })
            }) */
        },

    }
})

export default store
